<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Controller;
use think\Request;

class Project extends Backend
{
    protected $navTitle = "project";

    public function __construct()
    {
        parent::__construct();

        $this->ProjectModel = model('Project.Project');
        $this->ProjectOrderModel = model('Project.ProjectOrder');
    }


    public function index()
    {
        $projectList = $this->ProjectModel->paginate(10);

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'projectList' => $projectList
        ]);
    }

    // TODO 新增项目
    public function add()
    {
        $name = $this->request->param('add_name');

        $data = [
            'name' => $name
        ];

        $project = $this->ProjectModel->select();

        foreach ($project as $item) {
            if ($name == $item['name']) $this->error('项目名称已存在，请重新填写');
        }

        $result = $this->ProjectModel->save($data);

        if ($result === FALSE)  $this->error('新增项目失败');

        $this->success('新增项目成功');
    }

    public function save(Request $request)
    {
        //
    }

    // TODO 查询
    public function search()
    {
        $search_field = $this->request->param('search_field');

        $keyword = $this->request->param('keyword');

        $where = [];

        if ($search_field == 'name') $where['name'] = ['like', "%{$keyword}%"];

        $projectList = $this->ProjectModel->where($where)->paginate(10);

        return $this->fetch('index', [
            'navTitle' => $this->navTitle,
            'projectList' => $projectList
        ]);
    }

    // TODO 编辑
    public function edit()
    {
        $formData =  $this->request->param();

        $project = $this->ProjectModel->find($formData['id']);

        if (!$project) $this->error('项目不存在');

        $projects = $this->ProjectModel->select();

        foreach ($projects as $item) {
            if ($formData['name'] == $item['name']) $this->error('项目名称已存在,请重新填写');
        }

        $data = [
            'id' => $formData['id'],
            'name' => $formData['name']
        ];

        $result = $this->ProjectModel->isUpdate(true)->save($data);

        if ($result === FALSE) $this->error('项目修改失败,' . $this->ProjectModel->getError());

        $this->success('项目修改成功');
    }

    // TODO 真实删除
    public function delete()
    {
        $action = $this->request->param('action', '');

        if ($action == 'del') {
            $pid = $this->request->param('pid', '');

            $project = $this->ProjectModel->find($pid);

            if (!$project) $this->error('项目不存在');

            $result = $this->ProjectModel->where('id', $pid)->delete();

            if ($result === FALSE) {
                $this->error('项目删除失败，错误提示：' . $this->ProjectModel->getError());
            } else {
                $this->success('项目删除成功');
            }
        }

        if ($action == 'dels') {
            // TODO 接收所有数据
            $formData = $this->request->param();
            // TODO 转存idArr
            $idArr = $formData['idArr'];

            $result = $this->ProjectModel->whereIn('id', $idArr)->delete();

            $result === FALSE ? $this->error('项目删除失败') : $this->success('项目删除成功');
        }
    }
}
